﻿using UnityEngine;

namespace HyxFrame.Core
{

    public interface IUIBase
    {
        bool ismaxDepth { set; get; }
        string uiName { set; get; }
        int sortingOrder { set; get; }
        UILAYER uiLayer { set; get; }
        Transform cacheTransform { set; get; }
        Canvas cacheCanvas { set; get; }
        QuickVarList uiData { set; get; }
        GameObject uiPrefab { set; get; }
        void Init();
        void OnStart();
        void OnUpdate();
        void OnLateUpdate();
        void Register();
        void UnRegister();
        void Destroy();
        void OnClose();
    }

    public abstract class UIBase : MonoBehaviour
    {

        [HideInInspector]
        public bool ismaxDepth = false;
        [HideInInspector]
        public QuickVarList Data
        {
            get { return m_data; }
            set
            {
                if (m_data != null)
                    m_data.Dispose();
                m_data = value;
            }
        }
        protected QuickVarList m_data = null;

        [HideInInspector]
        public string uiName = string.Empty;
        [HideInInspector]
        public int sortingOrder = 0;
        [HideInInspector]
        public UILAYER uiLayer = UILAYER.NORMAL;

        //[SerializeField]
        //private TriggerListener[] m_helpButtons = default;


        private PanelCompleteHandler m_handler = null;

        [HideInInspector]
        public Transform cacheTransform;
        [HideInInspector]
        public Canvas cacheCanvas;



        private bool isIphone;
        private bool isHaveLiuhai;
        private RectTransform rectTransform;


        private int topSize = 100;
        private int bottomSize = 50;
        /// <summary>
        /// 判断横屏
        /// </summary>
        private bool LANDSCAPE;

        private float ScreenRatio;



        void SetPanelPos()
        {
            // 判断横竖屏
            LANDSCAPE = Screen.width > Screen.height ? true : false;

            // 判读屏幕比例
            ScreenRatio = (float)Screen.width / Screen.height;

            // 判断刘海屏
            if ((LANDSCAPE && ScreenRatio >= 2) || (!LANDSCAPE && (1 / ScreenRatio) >= 2))
            {
                isHaveLiuhai = true;
            }
            else
            {
                isHaveLiuhai = false;
            }


            if (isHaveLiuhai && (uiLayer == UILAYER.LOW || uiLayer == UILAYER.NORMAL))
            {
                if (!LANDSCAPE)
                {
                    rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, -topSize);
                    rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, bottomSize);
                }
                else
                {
                    rectTransform.offsetMax = new Vector2(-bottomSize, rectTransform.offsetMax.y);
                    rectTransform.offsetMin = new Vector2(bottomSize, rectTransform.offsetMin.y);
                }

            }


        }
        /// <summary>
        /// 需要的初始化
        /// </summary>
        /// <param name="args"></param>
        public void Initialization(UILAYER layer, QuickVarList args, PanelCompleteHandler handler = null)
        {
            if (cacheTransform == null)
            {
                uiLayer = layer;
                cacheTransform = transform;
                rectTransform = GetComponent<RectTransform>();
                isHaveLiuhai = false;
                Init();
                SetPanelPos();
            }

            uiName = cacheTransform.name;
            cacheCanvas = transform.parent.GetComponent<Canvas>();

            Data = args;
            UIStart();

            m_handler = handler;
        }

        private void Start()
        {

        }

        void UIStart()
        {
            OnStart();
        }

        void Update()
        {
            UnityEngine.Profiling.Profiler.BeginSample(uiName);
            OnUpdate();
            UnityEngine.Profiling.Profiler.EndSample();
        }

        void LateUpdate()
        {
            OnLateUpdate();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="args"></param>
        public void Refresh(QuickVarList args, PanelCompleteHandler handler = null)
        {
            Data = args;
            m_handler = handler;

            UnRegister();
            OnStart();
            Register();

        }

        public void UIClose()
        {
            OnClose();
        }


        public void UIDestroy()
        {

            Destroy();
            Data = null;

            m_handler = null;
        }


        protected T GetTargetComponent<T>(string targetPath) where T : Component
        {
            Transform targetTrans = cacheTransform.Find(targetPath);
            if (targetTrans == null)
                return null;
            return targetTrans.GetComponent<T>();
        }

        protected GameObject GetTargetGameObject(string targetPath)
        {
            Transform targetTrans = cacheTransform.Find(targetPath);
            if (targetTrans == null)
                return null;
            return targetTrans.gameObject;
        }

        protected T GetComponentInChildren<T>(Transform parent, string path) where T : Component
        {
            Transform targetTrans = parent.Find(path);
            if (targetTrans == null)
                return null;
            return targetTrans.GetComponent<T>();
        }

        #region 界面生命周期
        protected virtual void Init() { }

        protected virtual void OnStart() { Register(); }

        protected virtual void OnUpdate() { }

        protected virtual void OnLateUpdate() { }

        protected virtual void Register() { }

        protected virtual void UnRegister() { }

        protected virtual void OnClose() { UnRegister(); }

        protected virtual void Destroy() { }
        #endregion
    }
}